#include "lv_app.h"

static lv_obj_t *screen_1;
static lv_style_t style_cont_box, style_small_box;

static struct {
  uint32_t box_elec;
  uint32_t box_time;
  uint32_t box_bottom;
  uint32_t box_smoke;
  uint32_t box_fire;
  uint32_t box_3;
  uint32_t box_4;
  uint32_t box_temp;
  uint32_t box_rh;
  uint32_t box_7;
  uint32_t box_8;
} child_id_screen_1;

static void lv_app_style_init(void);
static uint32_t lv_app_box_elec_init(void);
static uint32_t lv_app_box_time_init(void);
static uint32_t lv_app_box_bottom_init(void);
static uint32_t lv_app_box_smoke_init(void);
static uint32_t lv_app_box_fire_init(void);
static uint32_t lv_app_box_3_init(void);
static uint32_t lv_app_box_4_init(void);
static uint32_t lv_app_box_temp_init(void);
static uint32_t lv_app_box_rh_init(void);
static uint32_t lv_app_box_7_init(void);
static uint32_t lv_app_box_8_init(void);

// #f6f5f4, #bdb5ae, #c6d5d1, #aeb5bd, #d8d8d8
void lv_app_init()
{
  /*screen 1 init*/
  screen_1 = lv_obj_create(NULL);
  lv_obj_set_size(screen_1, 800, 480);
  lv_obj_set_style_bg_color(screen_1, lv_color_hex(0xf6f5f4), 0);
  lv_obj_set_style_radius(screen_1, 0, 0);

  /*sytles*/
  lv_app_style_init();

  /*boxes*/
  child_id_screen_1.box_elec   = lv_app_box_elec_init();
  child_id_screen_1.box_time   = lv_app_box_time_init();
  child_id_screen_1.box_smoke  = lv_app_box_smoke_init();
  child_id_screen_1.box_fire   = lv_app_box_fire_init();
  child_id_screen_1.box_3      = lv_app_box_3_init();
  child_id_screen_1.box_4      = lv_app_box_4_init();
  child_id_screen_1.box_temp   = lv_app_box_temp_init();
  child_id_screen_1.box_rh      = lv_app_box_rh_init();
  child_id_screen_1.box_7      = lv_app_box_7_init();
  child_id_screen_1.box_8      = lv_app_box_8_init();
  child_id_screen_1.box_bottom = lv_app_box_bottom_init();

  /*final*/
  lv_scr_load(screen_1);
}

uint8_t lv_app_screen_1_set_small_box_text_fmt(screen_1_child_enum id, char *string, ...)
{
  lv_obj_t *screen_1_small_box_child;
  uint32_t uid;
  char text[50];
  va_list arg;

  switch (id) {
    case screen_1_box_smoke:
      uid = child_id_screen_1.box_smoke;
      break;
    case screen_1_box_fire:
      uid = child_id_screen_1.box_fire;
      break;
    case screen_1_box_3:
      uid = child_id_screen_1.box_3;
      break;
    case screen_1_box_4:
      uid = child_id_screen_1.box_4;
      break;
    case screen_1_box_temp:
      uid = child_id_screen_1.box_temp;
      break;
    case screen_1_box_rh:
      uid = child_id_screen_1.box_rh;
      break;
    case screen_1_box_7:
      uid = child_id_screen_1.box_7;
      break;
    case screen_1_box_8:
      uid = child_id_screen_1.box_8;
      break;
    default:
      return 0;
  }

  if ((screen_1_small_box_child = lv_obj_get_child(screen_1, uid)) == NULL) {
    return 0;
  }
  if ((screen_1_small_box_child = lv_obj_get_child(screen_1_small_box_child, -1)) == NULL) {
    return 0;
  }

  va_start(arg, string);
  vsprintf(text, string, arg); // todo:maybe have overflow problem
  va_end(arg);

  lv_label_set_text(screen_1_small_box_child, text);

  return 1;
}

static void lv_app_style_init(void)
{
  /*cont_box*/
  lv_style_init(&style_cont_box);
  lv_style_set_arc_rounded(&style_cont_box, 15);
  lv_style_set_bg_color(&style_cont_box, lv_color_hex(0xaeb5bd));
  lv_style_set_border_color(&style_cont_box, lv_color_hex(0xaeb5bd));

  /*small_box*/
  lv_style_init(&style_small_box);
  lv_style_set_arc_rounded(&style_small_box, 15);
  lv_style_set_border_width(&style_small_box, 0);
  lv_style_set_pad_all(&style_small_box, 2);
  lv_style_set_size(&style_small_box, 80);
  lv_style_set_bg_color(&style_small_box, lv_color_hex(0xd8d8d8));
  lv_style_set_border_color(&style_small_box, lv_color_hex(0xd8d8d8));
}

static uint32_t lv_app_box_elec_init(void)
{
  /*elec box*/
  // box
  lv_obj_t *box_elec = lv_obj_create(screen_1);

  lv_obj_add_style(box_elec, &style_cont_box, LV_PART_MAIN);
  lv_obj_set_size(box_elec, 300, 380);
  lv_obj_align_to(box_elec, screen_1, LV_ALIGN_OUT_LEFT_BOTTOM, 25 + 300, -40 - 10 - 25);

  return lv_obj_get_child_id(box_elec);
}

static uint32_t lv_app_box_time_init(void)
{
  // time
  lv_obj_t *box_time, *label_time, *label_date;

  box_time = lv_obj_create(screen_1);
  lv_obj_add_style(box_time, &style_small_box, LV_PART_MAIN);
  lv_obj_set_style_bg_color(box_time, lv_color_hex(0xc6d5d1), LV_PART_MAIN);
  lv_obj_set_style_border_color(box_time, lv_color_hex(0xc6d5d1), LV_PART_MAIN);
  lv_obj_set_size(box_time, 395, 160);
  lv_obj_set_scrollbar_mode(box_time, LV_SCROLLBAR_MODE_OFF);
  lv_obj_align_to(box_time, screen_1, LV_ALIGN_TOP_MID, 395 / 2 - 20, 25);

  label_time = lv_label_create(box_time);
  lv_obj_align(label_time, LV_ALIGN_CENTER, 0, -10);
  lv_obj_set_style_text_align(label_time, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
  lv_obj_set_style_text_font(label_time, &lv_font_montserrat_48, LV_PART_MAIN);
  lv_label_set_text(label_time, "11:22\n");
  label_date = lv_label_create(box_time);
  lv_obj_align(label_date, LV_ALIGN_CENTER, 0, 12);
  lv_obj_set_style_text_align(label_date, LV_TEXT_ALIGN_LEFT, LV_PART_MAIN);
  lv_obj_set_style_text_font(label_date, &lv_font_montserrat_20, LV_PART_MAIN);
  lv_label_set_text(label_date, "10-31-2025 Firday\n");

  return lv_obj_get_child_id(box_time);
}

static uint32_t lv_app_box_bottom_init(void)
{
  lv_obj_t *box_bottom = lv_obj_create(screen_1);
  lv_obj_add_style(box_bottom, &style_cont_box, LV_PART_MAIN);
  lv_obj_set_style_bg_color(box_bottom, lv_color_hex(0xbdb5ae), LV_PART_MAIN);
  lv_obj_set_style_border_color(box_bottom, lv_color_hex(0xbdb5ae), LV_PART_MAIN);
  lv_obj_set_size(box_bottom, 750, 40);
  lv_obj_align_to(box_bottom, screen_1, LV_ALIGN_BOTTOM_MID, 0, -10);
  lv_obj_set_scrollbar_mode(box_bottom, LV_SCROLLBAR_MODE_OFF);

  return lv_obj_get_child_id(box_bottom);
}

static uint32_t lv_app_box_smoke_init(void)
{
  lv_obj_t *box_smoke, *label_smoke, *label_smoke_value;

  box_smoke = lv_obj_create(screen_1);
  lv_obj_add_style(box_smoke, &style_small_box, LV_PART_MAIN);
  lv_obj_set_scrollbar_mode(box_smoke, LV_SCROLLBAR_MODE_OFF);
  lv_obj_align_to(box_smoke, screen_1, LV_ALIGN_BOTTOM_MID, 80 / 2 - 20, -10 - 40 - (40 + 25) * 2);

  label_smoke = lv_label_create(box_smoke);
  lv_obj_align(label_smoke, LV_ALIGN_TOP_MID, 0, 5);
  lv_obj_set_style_text_align(label_smoke, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
  lv_label_set_text(label_smoke, "Smoke\n\n");
  label_smoke_value = lv_label_create(box_smoke);
  lv_obj_align(label_smoke_value, LV_ALIGN_CENTER, 0, 3);
  lv_obj_set_style_text_align(label_smoke_value, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
  lv_obj_set_style_text_font(label_smoke_value, &lv_font_montserrat_20, LV_PART_MAIN);
  lv_label_set_text(label_smoke_value, "1450");

  return lv_obj_get_child_id(box_smoke);
}

static uint32_t lv_app_box_fire_init(void)
{
  lv_obj_t *box_fire, *label_fire, *label_fire_value;

  box_fire = lv_obj_create(screen_1);
  lv_obj_add_style(box_fire, &style_small_box, LV_PART_MAIN);
  lv_obj_set_scrollbar_mode(box_fire, LV_SCROLLBAR_MODE_OFF);
  lv_obj_align_to(box_fire, screen_1, LV_ALIGN_BOTTOM_MID, 80 / 2 - 20 + (80 + 25) * 1, -10 - 40 - (40 + 25) * 2);

  label_fire = lv_label_create(box_fire);
  lv_obj_align(label_fire, LV_ALIGN_TOP_MID, 0, 5);
  lv_obj_set_style_text_align(label_fire, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
  lv_label_set_text(label_fire, "Fire\n\n");
  label_fire_value = lv_label_create(box_fire);
  lv_obj_align(label_fire_value, LV_ALIGN_CENTER, 0, 3);
  lv_obj_set_style_text_align(label_fire_value, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
  lv_obj_set_style_text_font(label_fire_value, &lv_font_montserrat_20, LV_PART_MAIN);
  lv_label_set_text(label_fire_value, "null");

  return lv_obj_get_child_id(box_fire);
}

static uint32_t lv_app_box_3_init(void)
{
  lv_obj_t *box_3, *label_3, *label_3_value;

  box_3 = lv_obj_create(screen_1);
  lv_obj_add_style(box_3, &style_small_box, LV_PART_MAIN);
  lv_obj_set_scrollbar_mode(box_3, LV_SCROLLBAR_MODE_OFF);
  lv_obj_align_to(box_3, screen_1, LV_ALIGN_BOTTOM_MID, 80 / 2 - 20 + (80 + 25) * 2, -10 - 40 - (40 + 25) * 2);

  label_3 = lv_label_create(box_3);
  lv_obj_align(label_3, LV_ALIGN_TOP_MID, 0, 5);
  lv_obj_set_style_text_align(label_3, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
  lv_label_set_text(label_3, "\n\n");
  label_3_value = lv_label_create(box_3);
  lv_obj_align(label_3_value, LV_ALIGN_CENTER, 0, 3);
  lv_obj_set_style_text_align(label_3_value, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
  lv_obj_set_style_text_font(label_3_value, &lv_font_montserrat_20, LV_PART_MAIN);
  lv_label_set_text(label_3_value, "");

  return lv_obj_get_child_id(box_3);
}

static uint32_t lv_app_box_4_init(void)
{
  lv_obj_t *box_4, *label_4, *label_4_value;

  box_4 = lv_obj_create(screen_1);
  lv_obj_add_style(box_4, &style_small_box, LV_PART_MAIN);
  lv_obj_set_scrollbar_mode(box_4, LV_SCROLLBAR_MODE_OFF);
  lv_obj_align_to(box_4, screen_1, LV_ALIGN_BOTTOM_MID, 80 / 2 - 20 + (80 + 25) * 3, -10 - 40 - (40 + 25) * 2);

  label_4 = lv_label_create(box_4);
  lv_obj_align(label_4, LV_ALIGN_TOP_MID, 0, 5);
  lv_obj_set_style_text_align(label_4, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
  lv_label_set_text(label_4, "\n\n");
  label_4_value = lv_label_create(box_4);
  lv_obj_align(label_4_value, LV_ALIGN_CENTER, 0, 3);
  lv_obj_set_style_text_align(label_4_value, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
  lv_obj_set_style_text_font(label_4_value, &lv_font_montserrat_20, LV_PART_MAIN);
  lv_label_set_text(label_4_value, "");

  return lv_obj_get_child_id(box_4);
}

static uint32_t lv_app_box_temp_init(void)
{
  // temp
  lv_obj_t *box_temp, *label_temp, *label_temp_value;

  box_temp = lv_obj_create(screen_1);
  lv_obj_add_style(box_temp, &style_small_box, LV_PART_MAIN);
  lv_obj_set_scrollbar_mode(box_temp, LV_SCROLLBAR_MODE_OFF);
  lv_obj_align_to(box_temp, screen_1, LV_ALIGN_BOTTOM_MID, 80 / 2 - 20, -40 - 10 - 25);

  label_temp = lv_label_create(box_temp);
  lv_obj_align(label_temp, LV_ALIGN_TOP_MID, 0, 5);
  lv_obj_set_style_text_align(label_temp, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
  lv_label_set_text(label_temp, "Temp\n\n");
  label_temp_value = lv_label_create(box_temp);
  lv_obj_align(label_temp_value, LV_ALIGN_CENTER, 0, 3);
  lv_obj_set_style_text_align(label_temp_value, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
  lv_obj_set_style_text_font(label_temp_value, &lv_font_montserrat_20, LV_PART_MAIN);
  lv_label_set_text(label_temp_value, "...°C");

  return lv_obj_get_child_id(box_temp);
}

static uint32_t lv_app_box_rh_init(void)
{
  lv_obj_t *box_rh, *label_rh, *label_rh_value;

  box_rh = lv_obj_create(screen_1);
  lv_obj_add_style(box_rh, &style_small_box, LV_PART_MAIN);
  lv_obj_set_scrollbar_mode(box_rh, LV_SCROLLBAR_MODE_OFF);
  lv_obj_align_to(box_rh, screen_1, LV_ALIGN_BOTTOM_MID, 80 / 2 - 20 + (80 + 25) * 1, -40 - 10 - 25);

  label_rh = lv_label_create(box_rh);
  lv_obj_align(label_rh, LV_ALIGN_TOP_MID, 0, 5);
  lv_obj_set_style_text_align(label_rh, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
  lv_label_set_text(label_rh, "Humi\n\n");
  label_rh_value = lv_label_create(box_rh);
  lv_obj_align(label_rh_value, LV_ALIGN_CENTER, 0, 3);
  lv_obj_set_style_text_align(label_rh_value, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
  lv_obj_set_style_text_font(label_rh_value, &lv_font_montserrat_20, LV_PART_MAIN);
  lv_label_set_text(label_rh_value, "...RH");

  return lv_obj_get_child_id(box_rh);
}

static uint32_t lv_app_box_7_init(void)
{
  lv_obj_t *box_7, *label_7, *label_7_value;

  box_7 = lv_obj_create(screen_1);
  lv_obj_add_style(box_7, &style_small_box, LV_PART_MAIN);
  lv_obj_set_scrollbar_mode(box_7, LV_SCROLLBAR_MODE_OFF);
  lv_obj_align_to(box_7, screen_1, LV_ALIGN_BOTTOM_MID, 80 / 2 - 20 + (80 + 25) * 2, -40 - 10 - 25);

  label_7 = lv_label_create(box_7);
  lv_obj_align(label_7, LV_ALIGN_TOP_MID, 0, 5);
  lv_obj_set_style_text_align(label_7, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
  lv_label_set_text(label_7, "\n\n");
  label_7_value = lv_label_create(box_7);
  lv_obj_align(label_7_value, LV_ALIGN_CENTER, 0, 3);
  lv_obj_set_style_text_align(label_7_value, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
  lv_obj_set_style_text_font(label_7_value, &lv_font_montserrat_20, LV_PART_MAIN);
  lv_label_set_text(label_7_value, "");

  return lv_obj_get_child_id(box_7);
}

static uint32_t lv_app_box_8_init(void)
{
  lv_obj_t *box_8, *label_8, *label_8_value;

  box_8 = lv_obj_create(screen_1);
  lv_obj_add_style(box_8, &style_small_box, LV_PART_MAIN);
  lv_obj_set_scrollbar_mode(box_8, LV_SCROLLBAR_MODE_OFF);
  lv_obj_align_to(box_8, screen_1, LV_ALIGN_BOTTOM_MID, 80 / 2 - 20 + (80 + 25) * 3, -40 - 10 - 25);

  label_8 = lv_label_create(box_8);
  lv_obj_align(label_8, LV_ALIGN_TOP_MID, 0, 5);
  lv_obj_set_style_text_align(label_8, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
  lv_label_set_text(label_8, "\n\n");
  label_8_value = lv_label_create(box_8);
  lv_obj_align(label_8_value, LV_ALIGN_CENTER, 0, 3);
  lv_obj_set_style_text_align(label_8_value, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
  lv_obj_set_style_text_font(label_8_value, &lv_font_montserrat_20, LV_PART_MAIN);
  lv_label_set_text(label_8_value, "");

  return lv_obj_get_child_id(box_8);
}

// lv_obj_t *sub_box_1 = lv_obj_create(screen_1);
// lv_obj_add_style(sub_box_1, &style_cont_box, LV_PART_MAIN);
// lv_obj_set_style_bg_color(sub_box_1, lv_color_hex(0xd4d5c6), LV_PART_MAIN);
// lv_obj_set_style_border_color(sub_box_1, lv_color_hex(0xd4d5c6), LV_PART_MAIN);
// lv_obj_set_size(sub_box_1, 425, 380);
// lv_obj_align_to(sub_box_1, screen_1, LV_ALIGN_OUT_RIGHT_BOTTOM, -25 - 425, -40 - 10 - 25);

// label_smoke=lv_label_create(sub_box_1);
// label_fire=lv_label_create(sub_box_1);
